var request = require('request');
const fs = require('fs');
var SendRequest = function (type, data, callback) {

    if (data.requestType == 'form') {
        if (!data.url) {
            callback('URL 不能为空');
        };
        var headers = {};
        for (const key in data.header) {
            if (data.header.hasOwnProperty(key)) {
                const element = data.header[key];
                headers[key] = element;
            }
        };


        // var r = request.post(encodeURI(data.url));
        // var form = r.form();
        // form.append('my_field', 'my_value');
        // form.append('my_buffer', new Buffer([1, 2, 3]));
        // form.append('my_file', fs.createReadStream(path.join(__dirname, 'doodle.png')));

        var _data = {
            preambleCRLF: true,
            postambleCRLF: true,
            url: encodeURI(data.url),
            method: type,
            headers: headers,
            time: true,
            jar: true,
            formData: {},
            timeout: 10000,
        };
        for (let i = 0; i < data.formData.length; i++) {
            const item = data.formData[i];
            if (item.type == 'file') {
                _data.formData[item.key] = fs.createReadStream(item.value);
                // form.append(item.key, fs.createReadStream(item.value));
            } else {
                // form.append(item.key, item.value);
                _data.formData[item.key] = item.value;
            }
        };
        _data.url = _data.url.replace(/\%0A/g, '');
        // if (type == 'POST' || type == 'PUT') {
        //     _data.json = data.param;
        // };
        request(_data, function (error, response, body) {
            if (error) {
                callback(error, {}, data.RequestGuid);
            } else {
                var bodySize = 0;
                if (body && body.length > 1024) {
                    bodySize = body.length / 1024;
                    bodySize = bodySize.toFixed(2);
                } else if (body) {
                    bodySize = body.length;
                };
                callback(null, {
                    status: response.statusCode + ' ' + response.statusMessage,
                    size: bodySize,
                    body: body,
                    time: response.elapsedTime,
                }, data.RequestGuid);
            };
        });
    } else if (data.requestType == 'json') {
        if (!data.url) {
            callback('URL 不能为空');
        };
        var headers = {};
        for (const key in data.header) {
            if (data.header.hasOwnProperty(key)) {
                const element = data.header[key];
                headers[key] = element;
            }
        };
        var _data = {
            url: encodeURI(data.url),
            method: type,
            headers: headers,
            time: true,
            jar: true,
            timeout: 10000,
        };
        _data.url = _data.url.replace(/\%0A/g, '');

        if (type == 'POST' || type == 'PUT') {
            _data.json = data.param;
        };
        request(_data, function (error, response, body) {
            if (error) {
                callback(error, {}, data.RequestGuid);
            } else {
                var bodySize = 0;
                if (body && body.length > 1024) {
                    bodySize = body.length / 1024;
                    bodySize = bodySize.toFixed(2);
                } else if (body) {
                    bodySize = body.length;
                };
                callback(null, {
                    status: response.statusCode + ' ' + response.statusMessage,
                    size: bodySize,
                    body: body,
                    time: response.elapsedTime,
                }, data.RequestGuid);
            };
        });
    };
};

module.exports.POST = function (data, callback) {
    SendRequest('POST', data, callback);
};
module.exports.GET = function (data, callback) {
    SendRequest('GET', data, callback);
};
module.exports.PUT = function (data, callback) {
    SendRequest('PUT', data, callback);
};
module.exports.DEL = function (data, callback) {
    SendRequest('DELETE', data, callback);
};